import axios from "axios";
import { Message } from "element-ui";
import Cookies from "js-cookie";
// import { getToken, getEncrypt } from "@/utils/auth";
import syncConfig from "./syncConfig.js";
// import store from "@/store";
// import { sm4, sm2 } from "sm-crypto";

// create an axios instance
const service = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  baseURL: Cookies.get("lan") == 1 ? syncConfig.ythUrl : syncConfig.ythUrl
});
const YthToken = Cookies.get('Admin-Token')
// request interceptor
service.interceptors.request.use(
  config => {
    if (config.params) {
      config.params.accessToken = YthToken;
    } else if (config.data) {
      config.data.accessToken = YthToken;
    } else {
      config.params = {
        accessToken: YthToken
      };
    }
    config.headers["accessToken"] = YthToken;
    return config;
  },
  error => {
    // do something with request error
    console.log(error); // for debug
    return Promise.reject(error);
  }
);

// response interceptor
service.interceptors.response.use(
  response => {
    return response.data;
  },
  error => {
    console.log(error); // for debug
    Message({
      message: "服务器没有响应，请稍后重试！",
      type: "error",
      showClose: true,
      duration: 5 * 1000
    });
    return Promise.reject(error);
  }
);

export default service;
